package com.hqyj.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @description:
 * @author: GP
 * @date: 2021/9/15-13:59
 */
@Component
public class MyEmailUtil {
    @Value("${spring.mail.username}")
    private String myEmailNum;

    public String getMyEmailNum() {
        return myEmailNum;
    }

    //注入邮件发送类
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送邮件
     * @param email   收件人的邮箱号
     * @param content  邮件内容
     * @param title    邮件标题
     * @return
     */
    public HashMap<String,Object> sendEmail(String email,String content,String title){
        HashMap<String,Object > map  = new HashMap();
        //创建一个邮件
        SimpleMailMessage message  = new SimpleMailMessage();
        //设置发件人邮箱
        message.setFrom(myEmailNum);
        //设置收件人邮箱
        message.setTo(email);
        //设置邮件标题
        message.setSubject(title);
        //设置邮件正文
        message.setText(content);

        //发送邮件
        try {
            javaMailSender.send(message);
            map.put("info","发送成功");
        } catch (MailException e) {
            e.printStackTrace();
            map.put("info","发送失败");
        }

        return  map;
    }
}
